//
//  UserOrderRateViewController.m
//  demoapp
//
//  Created by MingmingSun on 15/9/27.
//  Copyright (c) 2015年 pingplusplus. All rights reserved.
//

#import "UserOrderRateViewController.h"
#import "RatingBar.h"

@interface UserOrderRateViewController ()

@property(nonatomic,strong) RatingBar *rb1;
@property(nonatomic,strong) RatingBar *rb2;
@property(nonatomic,strong) RatingBar *rb3;

@property(nonatomic,weak) IBOutlet UIImageView *headImg;
@property(nonatomic,weak) IBOutlet UILabel *lab1;
@property(nonatomic,weak) IBOutlet UILabel *lab2;
@property(nonatomic,weak) IBOutlet UILabel *lab3;
@property(nonatomic,weak) IBOutlet UITextField *tf;

-(IBAction)buttonPressed:(id)sender;

@end

@implementation UserOrderRateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"评价订单";
        self.navigationItem.leftBarButtonItem =
        [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"arrow_white"]
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(backPressed:)];
    }
    return self;
}

-(void)backPressed:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rb1 = [[RatingBar alloc] initWithFrame:CGRectMake(80,88, 180, 30)];
    [self.view addSubview:self.rb1];
    self.rb1.starNumber = 3;
    self.rb2 = [[RatingBar alloc] initWithFrame:CGRectMake(80,126, 180, 30)];
    [self.view addSubview:self.rb2];
    self.rb2.starNumber = 3;
    self.rb3 = [[RatingBar alloc] initWithFrame:CGRectMake(80,164, 180, 30)];
    [self.view addSubview:self.rb3];
    self.rb3.starNumber = 3;
    
    self.lab1.text = [tooles replaceNullFromDic:self.orderDic withKeyArray:@[@"barber"]];
    self.lab2.text = [NSString stringWithFormat:@"总价:￥%@",[tooles replaceNullFromDic:self.orderDic withKeyArray:@[@"dealPrice"]]];
    self.lab3.text = [tooles replaceNullFromDic:self.orderDic withKeyArray:@[@"payTime"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(IBAction)buttonPressed:(id)sender{
    int rb1value = self.rb1.starNumber;
    int rb2value = self.rb2.starNumber;
    int rb3value = self.rb3.starNumber;
    int sumvalue = (rb1value + rb2value + rb3value - 1)/3 + 1;
    NSDictionary *para =
    @{@"id":[NSString stringWithFormat:@"%i",[[self.orderDic valueForKey:@"id"] integerValue]],
      @"score":[NSString stringWithFormat:@"%i",sumvalue * 20],
      @"profession":[NSString stringWithFormat:@"%i",rb1value * 20],
      @"communicate":[NSString stringWithFormat:@"%i",rb2value * 20],
      @"punctual":[NSString stringWithFormat:@"%i",rb3value * 20],
      @"remark":self.tf.text,
      @"token":APPALL.myUserItem.token};
    [SVProgressHUD showWithStatus:@"loading..."];
    [AVCloud callFunctionInBackground:@"order_markBarber" withParameters:para block:^(id object, NSError *error) {
        if (!error && object) {
            NSLog(@"avget:%@",object);
            NSDictionary *aSucData = [NSJSONSerialization JSONObjectWithData: [object dataUsingEncoding:NSUTF8StringEncoding] options: NSJSONReadingMutableLeaves error: &error];
            switch ([[aSucData valueForKey:@"success"] intValue]) {
                case 0:
                {
                    [tooles showGlobalAlertWithTitle:@"" andMsg:[aSucData valueForKey:@"msg"] delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
                    [self.navigationController popViewControllerAnimated:YES];
                }
                    break;
                case 1:
                {
                    NSLog(@"yeah1....");
                    [self.myDelegate rateFinished:nil];
                }
                    break;
                default:
                    break;
            }
            [SVProgressHUD dismiss];
        } else {
            [SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"%@", error]];
        }
    }];
}

@end
